﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace AISFMIOperator
{
    class Facility
    {
        public Facility()
        { }

        public int FacilityID
        { get; set; }

        public string FacilityName
        { get; set; }

        public string Description
        { get; set; }

        public Address Address
        { get; set; }

        public FacilityType FacilityType
        { get; set; }

        public string FullAddress
        {
            get
            {
                return Address.FullAddress;
            }
        }

        public string FacilityTypeName
        { get { return FacilityType.FacilityTypeName; } }

        public void Save()
        {
            SqlCommand command = new SqlCommand("CreateFacility");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@facility_name", SqlDbType.VarChar, 128).Value = FacilityName;
            command.Parameters.Add("@description", SqlDbType.VarChar, 256).Value = Description;
            command.Parameters.Add("@facility_type_id", SqlDbType.Int).Value = FacilityType.FacilityTypeID;
            command.Parameters.Add("@address_id", SqlDbType.Int).Value = Address.AddressID;
            command.Parameters.Add("@facility_id", SqlDbType.Int).Direction = ParameterDirection.Output;
            SQLDBLayer.ExecSPNonQuery(command);
            this.FacilityID = (int)command.Parameters["@facility_id"].Value;
        }

        public void Update()
        {
            SqlCommand command = new SqlCommand("UpdateFacility");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@facility_name", SqlDbType.VarChar, 128).Value = FacilityName;
            command.Parameters.Add("@description", SqlDbType.VarChar, 256).Value = Description;
            command.Parameters.Add("@facility_type_id", SqlDbType.Int).Value = FacilityType.FacilityTypeID;
            //command.Parameters.Add("@address_id", SqlDbType.Int).Value = Address.AddressID;
            command.Parameters.Add("@facility_id", SqlDbType.Int).Value = FacilityID;
            SQLDBLayer.ExecSPNonQuery(command);
        }

        public void Delete()
        {
            SqlCommand command = new SqlCommand("DeleteFacility");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@facility_id", SqlDbType.Int).Value = FacilityID;
            SQLDBLayer.ExecSPNonQuery(command);
        }

        public static List<Facility> GetAllFacilities()
        {
            DataTable table = new DataTable();
            List<Facility> facilities = new List<Facility>();
            SqlCommand command = new SqlCommand("GetFacilities");
            command.CommandType = CommandType.StoredProcedure;

            table = SQLDBLayer.ExecSPTable(command);

            foreach (DataRow row in table.Rows)
            {
                Facility facility = new Facility();
                facility.FacilityID = (int)row["facility_id"];
                facility.FacilityName = (string)row["facility_name"];
                facility.Description = (string)row["description"];

                int address_id = (int)row["address_id"];
                int facility_type_id = (int)row["facility_type_id"];

                facility.Address = (Address)Address.GetAllAddresses().Where(o => o.AddressID == address_id).ToList()[0];
                facility.FacilityType = (FacilityType)FacilityType.GetAllFacilityTypes().Where(o => o.FacilityTypeID == facility_type_id).ToList()[0];

                facilities.Add(facility);
            }

            return facilities;
        }
    }
}
